//
//  Joseph.c
//  algorithmDemo
//
//  Created by 李文深 on 16/7/10.
//  Copyright © 2016年 30pay. All rights reserved.
//   约瑟夫环

#include "Joseph.h"
#include <stdio.h>
#include <stdlib.h>



List create(int num) {
    
    List head = (List)malloc(sizeof(struct Node));
    head->next = NULL;
    
    List q = head;
    
    for (int i = 1; i<= num; i++) {
        
        List ele = (List)malloc(sizeof(struct Node));
        ele->next = NULL;
        ele->data = i;
        
        q->next = ele;
        q = ele;
        
    }
    
    q->next = head->next;
    
    free(head);
    
    return q->next;
}

void showOrders(List l, int m, int n) {
    
    List p = l;
    
    List temp;
    
    n %= m;
    
    while (p != p->next) {
        for (int i = 1; i < n-1; i++) {
            p = p->next;
        }
        printf("%d-->",p->next->data);
        temp = p->next;
        p->next = temp->next;
        free(temp);
        p = p->next;
    }
    
    printf("%d\n",p->data);
    
}

void printJoseph(List l) {
    
    List p,q;
    p = q = l;
    
    do {
        printf("元素%d\n",p->data);
        p = p->next;
        
    } while(p != q);
    

}